/**
 * @param {number[]} nums
 * @return {number}
 * 输入：nums = [100,4,200,1,3,2]
输出：4
解释：最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
 */
var longestConsecutive = function (nums) {
  let max = 0
  const obj = {}
  nums.forEach(item => {
    if (obj[item]) return
    obj[item] = 1
    if (obj[item - 1]) {
      obj[item] = obj[item - 1] + 1
    }
    let index = item
    time = 0
    while (obj[index + 1]) {
      index++
      time++
    }
    obj[index] = obj[item] + time
    max = Math.max(max, obj[index])
  })
  return max
}
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]))
